package com.wxf.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * 消费端限流
 * 把配置文件中的prefetch设置为n，表示一次拉取n条数据
 * 确认签收后再去进行下一次的数据拉取
 */
@Component
public class PrefetchListener implements ChannelAwareMessageListener {
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        // 线程睡眠观察效果
        Thread.sleep(1000);

        System.out.println(message.getBody());

        // 手动确认签收消息，再去拉取下一条消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
    }
}
